package com.ayitubie.refuelweb.controller;

import lombok.extern.slf4j.Slf4j;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.Project;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/gitlab")
@Configuration
public class GitLabCtl {
    @Value("${gitlab.url}")
    private String url;

    @Value("${gitlab.username}")
    private String username;

    @Value("${gitlab.password}")
    private String password;

    @GetMapping("getProjects")
    public List getGitLabProjects(){
        List<Project> projects = new ArrayList<>();
        try {
            GitLabApi gitLabApi = GitLabApi.oauth2Login(url, username, password);
            projects = gitLabApi.getProjectApi().getProjects();
        } catch (GitLabApiException e) {
            e.printStackTrace();
        }
        return projects;
    }
}
